package com.forum.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.forum.pojo.ArticleTipoffInfo;
import com.forum.pojo.StudentInfo;
import com.forum.pojo.UserTipoffInfo;
import com.forum.result.Result;
import com.forum.service.ArticleTipoffInfoService;
import com.forum.service.CommentTipoffInfoService;
import com.forum.service.StudentInfoService;
import com.forum.service.UserTipoffInfoService;
import com.forum.utils.JwtHelper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/tipoff")
@CrossOrigin
public class TipOffController {
    @Autowired
    ArticleTipoffInfoService articleTipoffInfoService;
    @Autowired
    CommentTipoffInfoService commentTipoffInfoService;
    @Autowired
    UserTipoffInfoService userTipInfoService;
    @Autowired
    StudentInfoService studentInfoService;
    @Autowired
    RedisTemplate redisTemplate;
    @PostMapping("/articletipoff")
    public void saveArticleTipOff(@RequestParam String articleId, @RequestParam String description, HttpServletRequest request){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        articleTipoffInfoService.saveTipOff(userId,articleId,description);
    }
    @PostMapping("/commentipoff")
    public void saveCommentTipOff(@RequestParam Integer commentId,@RequestParam String description,HttpServletRequest request){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        commentTipoffInfoService.saveTipOff(userId,commentId,description);
    }
    @PostMapping("/usertipoff")
    public void saveUserTipOff(@RequestParam Long informeduserId,@RequestParam String description,HttpServletRequest request){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        userTipInfoService.saveTipOff(userId,informeduserId,description);
    }
    @ApiOperation("获取所有举报")
    @GetMapping("/getAllTipoff")
    public Result getAllTipOff(@RequestParam Integer type,@RequestParam(required = false) Integer searchType,@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        switch (type){
            case 1:
                return Result.ok(articleTipoffInfoService.getTipOff(searchType,pageNo,pageSize));
            case 2:
                return Result.ok(commentTipoffInfoService.getTipOff(pageNo,pageSize));
            case 3:
                return Result.ok(userTipInfoService.getTipOff(pageNo,pageSize));
            default:
                return Result.fail();
        }
    }
    @ApiOperation("忽略举报")
    @DeleteMapping("/ignore")
    public Result ignore(@RequestParam Long tipoffId, @RequestParam Long informedUserId,@RequestParam Integer type){
        boolean flag=false;
        studentInfoService.lessCount(informedUserId);
            switch (type){
                case 1:
                    flag=articleTipoffInfoService.removeById(tipoffId);
                    break;
                case 2:
                    flag=commentTipoffInfoService.removeById(tipoffId);
                    break;
                case 3:
                    flag=userTipInfoService.removeById(tipoffId);
                    break;
            }
            if(flag){
                return Result.ok();
            }else{
                return Result.build(320,"删除失败");
            }
    }

}
